<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WSL+VSCode开发USB设备驱动环境配置指南</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #252;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4efe9 100%);
            padding-top: 80px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        /* 固定顶部导航栏样式 */
        .fixed-nav {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background: #2c3e50;
            padding: 15px 0;
            z-index: 1000;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
        
        .nav-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            padding: 0 20px;
        }
        
        .nav-title {
            color: white;
            margin-right: 20px;
            font-size: 1.4rem;
            font-weight: 600;
            display: flex;
            align-items: center;
        }
        
        .nav-links {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 8px;
        }
        
        .nav-item {
            list-style: none;
        }
        
        .nav-link {
            display: block;
            padding: 10px 18px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 500;
            font-size: 0.95rem;
            transition: all 0.3s ease;
            color: white;
            text-align: center;
            min-width: 100px;
        }
        
        /* 为每个导航项设置不同颜色 */
        .nav-item:nth-child(1) .nav-link { background-color: #e74c3c; }
        .nav-item:nth-child(2) .nav-link { background-color: #3498db; }
        .nav-item:nth-child(3) .nav-link { background-color: #2ecc71; }
        .nav-item:nth-child(4) .nav-link { background-color: #f39c12; }
        .nav-item:nth-child(5) .nav-link { background-color: #9b59b6; }
        .nav-item:nth-child(6) .nav-link { background-color: #1abc9c; }
        .nav-item:nth-child(7) .nav-link { background-color: #d35400; }
        .nav-item:nth-child(8) .nav-link { background-color: #7f8c8d; }
        
        .nav-link:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }
        
        /* 页面内容样式 */
        .content {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
            margin-bottom: 30px;
        }
        
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }
        
        h2 {
            color: #2c3e50;
            margin: 25px 0 15px;
            padding-left: 12px;
            border-left: 5px solid #3498db;
        }
        
        h3 {
            color: #2c3e50;
            margin: 20px 0 10px;
            padding-left: 8px;
            border-left: 3px solid #2ecc71;
        }
        
        p {
            margin-bottom: 15px;
            line-height: 1.7;
        }
        
        .code-block {
            background: #2c3e50;
            color: #f8f8f2;
            padding: 18px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 20px 0;
            font-family: 'Fira Code', monospace;
            font-size: 0.95rem;
            line-height: 1.5;
            white-space: pre-wrap;
        }
        
        .note {
            background-color: #e7f3fe;
            border-left: 4px solid #2196F3;
            padding: 15px;
            margin: 20px 0;
            border-radius: 4px;
        }
        
        .warning {
            background-color: #fff8e1;
            border-left: 4px solid #ffc107;
            padding: 15px;
            margin: 20px 0;
            border-radius: 4px;
        }
        
        .tip {
            background-color: #e8f5e9;
            border-left: 4px solid #4caf50;
            padding: 15px;
            margin: 20px 0;
            border-radius: 4px;
        }
        
        ol, ul {
            margin-left: 20px;
            margin-bottom: 20px;
        }
        
        li {
            margin-bottom: 10px;
        }
        
        code {
            background-color: #f5f5f5;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Fira Code', monospace;
            color: #d63384;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .nav-container {
                flex-direction: column;
                align-items: center;
            }
            
            .nav-title {
                margin-right: 0;
                margin-bottom: 10px;
            }
            
            .nav-links {
                width: 100%;
                justify-content: space-around;
            }
            
            .nav-link {
                padding: 8px 12px;
                font-size: 0.85rem;
                min-width: auto;
            }
            
            body {
                padding-top: 120px;
            }
        }
    </style>
</head>
<body>
    <!-- 固定顶部导航栏 -->
    <nav class="fixed-nav">
        <div class="nav-container">
            <div class="nav-title"></div>
            <ul class="nav-links">
                <li class="nav-item"><a href="#wsl-reset" class="nav-link">重置WSL</a></li>
                <li class="nav-item"><a href="#ubuntu-install" class="nav-link">安装Ubuntu</a></li>
                <li class="nav-item"><a href="#usb-driver" class="nav-link">USB驱动</a></li>
                <li class="nav-item"><a href="#usb-connection" class="nav-link">USB连接</a></li>
                <li class="nav-item"><a href="#hk-daq" class="nav-link">采集卡配置</a></li>
                <li class="nav-item"><a href="#sudo-solution" class="nav-link">USB权限</a></li>
                <li class="nav-item"><a href="#vscode-setup" class="nav-link">VSCode配置</a></li>
                <li class="nav-item"><a href="#summary" class="nav-link">步骤小结</a></li>
            </ul>
        </div>
    </nav>

    <div class="container">
        <div class="content">
            <h1>WSL+VSCode开发USB设备驱动环境配置指南</h1>
            
            <h2 id="wsl-reset">WSL 归零与重置</h2>
            <p>如果需要重新开始或解决WSL问题，可以按照以下步骤重置WSL环境：</p>
            <div class="code-block"># 列出已安装的发行版：
wsl --list --verbose

# 逐个注销（删除）发行版：
wsl --unregister &lt;发行版名称&gt;

# 例如：
wsl --unregister Ubuntu-20.04

# 重置默认版本为 WSL2：
wsl --set-default-version 2</div>

            <h2 id="ubuntu-install">安装Ubuntu</h2>
            <p>使用以下命令安装Ubuntu到指定目录：</p>
            <div class="code-block">mkdir d:\wsl\ubuntu-24.04
wsl --import ubuntu d:\wsl\ubuntu-24.04 F:\Ubuntu_WSL_Prepare\ubuntu-noble-wsl-amd64-24.04lts.rootfs.tar.gz</div>
            
            <p>配置清华镜像源以加速软件下载：</p>
            <div class="code-block">if [ -f '/etc/apt/sources.list.d/ubuntu.sources' ]; then
    cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.backup
    echo '已备份原有源文件'
fi

sudo tee /etc/apt/sources.list.d/ubuntu.sources > /dev/null << 'EOF'
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble-proposed
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF</div>

            <p>更新系统并安装常用工具：</p>
            <div class="code-block"># 更新系统包列表
apt update

# 升级已安装的包
apt upgrade -y

# 安装常用工具
apt install -y curl wget git vim htop build-essential usbutils linux-tools-virtual hwdata</div>

            <p>更改默认用户：</p>
            <div class="code-block">sudo nano /etc/wsl.conf</div>
            <p>在文件中添加以下内容，将<code>YourUsername</code>替换为你的用户名：</p>
            <div class="code-block">[user]
default = YourUsername</div>
            <p>保存并退出文件：按<code>Ctrl + X</code>，然后按<code>Y</code>，最后按<code>Enter</code>。然后完全重启WSL：</p>
            <div class="code-block">wsl --shutdown
wsl</div>

            <h2 id="usb-driver">安装USB驱动</h2>
            <p>在Windows端安装usbipd：</p>
            <div class="code-block"># 以管理员身份运行PowerShell
winget install usbipd</div>

            <p>在WSL中安装必要的工具：</p>
            <div class="code-block">sudo apt install linux-tools-generic hwdata -y</div>

            <p>创建符号链接，确保系统能找到正确版本的usbip命令：</p>
            <div class="code-block"># 查看已安装的linux-tools版本
ls /usr/lib/ | grep linux-tools

# 创建符号链接 (将'xx'替换为实际的目录名)
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools-6.8.0-xx/usbip 20</div>

            <p>验证安装是否成功：</p>
            <div class="code-block">usbip version</div>
            <p>如果正确显示了版本信息（如<code>usbip version 2.0</code>），则说明安装成功。</p>

            <h2 id="usb-connection">连接和使用USB设备</h2>
            <h3>1. 列出Windows上的USB设备</h3>
            <p>在Windows PowerShell中运行：</p>
            <div class="code-block">usbipd list</div>
            <p>这个命令会列出所有连接到Windows的USB设备，记下你的USB设备对应的BUSID。</p>

            <h3>2. 绑定（共享）设备</h3>
            <div class="warning">
                <p>重要：在绑定前，请确保任何Windows软件都没有占用这个USB设备（例如设备管理器里没有显示在使用中）。</p>
            </div>
            <p>在Windows PowerShell（管理员身份）中，运行以下命令来绑定设备：</p>
            <div class="code-block">usbipd bind --busid &lt;BUSID&gt;</div>
            <p>将<code>&lt;BUSID&gt;</code>替换为你设备的总线ID，成功后会提示<code>Device bound to usbipd</code>。</p>
            
            <p>运行以下命令attach USB设备，获得Windows下的IP地址：</p>
            <div class="code-block">usbipd attach --wsl ubuntu --busid &lt;BUSID&gt;</div>
            <p>输出类似：<code>usbipd: info: Using IP address 172.28.160.1 to reach the host.</code></p>

            <h3>3. 在WSL2中连接设备</h3>
            <div class="code-block">sudo usbip attach -r 172.28.160.1 -b &lt;BUSID&gt;</div>
            <p>将<code>&lt;BUSID&gt;</code>替换为你的设备总线ID。</p>

            <h3>4. 断开连接</h3>
            <p>在Ubuntu终端中：</p>
            <div class="code-block"># 列出已挂载的设备
usbip port

# 断开指定端口
sudo usbip detach --port &lt;Port号&gt;</div>
            
            <p>在Windows PowerShell（管理员）中，解除设备的绑定：</p>
            <div class="code-block">usbipd unbind --busid &lt;BUSID&gt;</div>
            <p>解除绑定后，设备就可以重新被Windows系统识别了。</p>

            <h2 id="hk-daq">恒凯采集卡配置</h2>
            <p>安装USB驱动：</p>
            <div class="code-block">sudo apt install libusb-1.0-0-dev</div>
            
            <p>编译libUSB_Daq_V52_32.so：</p>
            <div class="code-block">cd /path_to_&lt;libusb1.0-daq-v5.2-32-dev/so&gt;
make clean
make
cp ./libUSB_Daq_V52_32.co /usr/lib</div>
            
            <p>编译示例程序：</p>
            <div class="code-block">cd /path_to_&lt;libusb1.0-daq-v5.2-32-dev/test&gt;
make clean
make
sudo chmod +x ./usb_daq_test
sudo ./usb_daq_test # 需要使用sudo才能访问USB</div>

            <h2 id="sudo-solution">解决需要sudo调用USB的问题</h2>
            <p>由于程序需要访问USB设备，通常需要root权限。推荐配置udev规则使普通用户也能访问设备：</p>
            
            <p>确定设备的厂商ID和产品ID：</p>
            <div class="code-block">lsusb</div>
            
            <p>创建udev规则文件：</p>
            <div class="code-block">sudo nano /etc/udev/rules.d/99-usb-daq.rules</div>
            
            <p>添加规则（替换为您设备的vendor和product ID）：</p>
            <div class="code-block">SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="5678", MODE="0666"</div>
            
            <p>保存退出，然后重新加载udev规则：</p>
            <div class="code-block">sudo udevadm control --reload-rules
sudo udevadm trigger</div>
            
            <p>检查设备节点权限：</p>
            <div class="code-block">ls -l /dev/bus/usb/XXX/YYY</div>
            
            <p>其中XXX、YYY由lsusb命令得到：</p>
            <div class="code-block">Bus 001 Device 002: ID 04b4:7809 Cypress Semiconductor Corp. USB-DAQ-CARD
    XXX        YYY</div>
            
            <p>正确结果：<code>crw-rw-rw- 1 root root 189, 1 Aug 26 00:45 /dev/bus/usb/001/002</code></p>
            <p>错误结果：<code>crw-rw-r-- 1 root root 189, 1 Aug 26 00:31 /dev/bus/usb/001/002</code></p>

            <h2 id="vscode-setup">VSCode + WSL开发环境配置</h2>
            <p>配置Windows VSCode用于WSL开发：</p>
            
            <h3>1. 安装必要组件</h3>
            <p>在WSL中安装编译工具链：</p>
            <div class="code-block">sudo apt update && sudo apt install build-essential gdb cmake
sudo apt install python3 python3-pip</div>
            
            <p>在VSCode中安装Microsoft WSL扩展。</p>
            
            <h3>2. 连接VSCode到WSL</h3>
            <p>打开VSCode，点击左下角的&gt;&lt;图标，选择"Connect to WSL"。</p>
            
            <h3>3. 从WSL中打开项目</h3>
            <div class="code-block">cd /path_to_your_prj/
code .</div>
            
            <div class="note">
                <p>注意：如果<code>code .</code>命令不起作用，可能需要重启终端，或者在安装VSCode时没有将其添加到PATH中。</p>
            </div>

            <h2 id="summary">步骤小结</h2>
            <ol>
                <li>打开PowerShell，启动WSL：<code>wsl</code></li>
                <li>列出和绑定USB设备：<code>usbipd list</code>、<code>usbipd bind</code>、<code>attach --busid &lt;busid&gt;</code></li>
                <li>进入项目文件夹：<code>cd /prj_folder</code></li>
                <li>使用VSCode打开项目：<code>code .</code></li>
                <li>打开终端（如Terminator）</li>
                <li>检查USB端口：<code>usbip port</code></li>
                <li>使用GDB调试程序：<code>gdb</code></li>
            </ol>
        </div>
    </div>

    <script>
        // 添加平滑滚动效果
        document.querySelectorAll('.nav-link').forEach(anchor => {
            anchor.addEventListener('click', function(e) {
                e.preventDefault();
                const targetId = this.getAttribute('href');
                const targetElement = document.querySelector(targetId);
                
                window.scrollTo({
                    top: targetElement.offsetTop - 80,
                    behavior: 'smooth'
                });
                
                // 更新URL哈希
                history.pushState(null, null, targetId);
            });
        });
        
        // 添加导航项激活状态
        window.addEventListener('scroll', function() {
            const sections = document.querySelectorAll('h2');
            const navLinks = document.querySelectorAll('.nav-link');
            
            let currentSection = '';
            
            sections.forEach(section => {
                const sectionTop = section.offsetTop;
                if (pageYOffset >= sectionTop - 100) {
                    currentSection = section.getAttribute('id');
                }
            });
            
            navLinks.forEach(link => {
                link.classList.remove('active');
                if (link.getAttribute('href') === '#' + currentSection) {
                    link.classList.add('active');
                }
            });
        });
    </script>
</body>
</html>